package com.example.myapplication;

import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.PopupWindow;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import com.example.myapplication.adapters.MyAdapter;
import com.example.myapplication.tools.DataTool;

public class DialogPopActivity extends AppCompatActivity {

    private PopupWindow popupWindow;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);

        ListView listView = findViewById(R.id.lv);
        listView.setAdapter(new MyAdapter(DataTool.getUserInfo(20), this));

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            }
        });

    }

    public void btnDialog(View view) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setIcon(R.drawable.ic_baseline_face_24)
                .setTitle("郑重提示")
                .setMessage("其实也没啥说的")
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                })
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                })
                .setNeutralButton("信息", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                })
                .setCancelable(false)     //禁止点蒙版进行关闭对话框
                .create()   //创建
                .show();    //显示
    }

    public void btnPopupWindow(View view) {
        //获取要弹出的view
        View popupView = getLayoutInflater().inflate(R.layout.popup_window, null);

        //组装成popupWindow
        popupWindow = new PopupWindow(popupView, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);

        //显示在下面
        popupWindow.showAsDropDown(view, view.getWidth() / 2 - 120, 0);

        popupView.findViewById(R.id.beijing).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                closePopup(popupWindow);
            }
        });

        popupView.findViewById(R.id.shanghai).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                closePopup(popupWindow);
            }
        });


        popupView.findViewById(R.id.tianjin).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                closePopup(popupWindow);
            }
        });

    }

    private void closePopup(PopupWindow popupWindow) {
        popupWindow.dismiss();
    }

}